package com.substation.ui.fragment;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.greenentity.Defect;
import com.greenentity.Eqpt;
import com.greenentity.Inspect;
import com.greenentity.Photo;
import com.greenentity.Task;
import com.greenentity.Unit;
import com.jessewu.library.SuperAdapter;
import com.jessewu.library.view.ViewHolder;
import com.substation.R;
import com.substation.greendao.DefectDao;
import com.substation.greendao.EqptDao;
import com.substation.greendao.InspectDao;
import com.substation.greendao.PhotoDao;
import com.substation.ui.activity.DefectAddActivity;
import com.substation.ui.activity.PatroDetailActivity;
import com.substation.ui.activity.RemoveDefectActivity;
import com.substation.ui.adapter.DefectImageAdapter;
import com.substation.ui.utils.RecyclerViewUtils;
import com.substation.utils.GreenDaoManager;
import com.substation.utils.SPUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by admin on 2018/5/17.
 */

public class DefectFragment extends BaseFragment {
    @BindView(R.id.list)
    RecyclerView list;

    SuperAdapter<Defect> adapter;
    List<Defect> datas = new ArrayList<>();
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.totalCount)
    TextView totalCount;
    @BindView(R.id.readCount)
    TextView readCount;
    Unit unit;
    Task task;
    int total = 0;
    int ready = 0;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_defect;
    }

    @Override
    protected void initView() {
        unit = JSON.parseObject((String) SPUtils.get(context, "unit", ""), Unit.class);
        task = JSON.parseObject((String) SPUtils.get(context, "task", ""), Task.class);
        name.setText((String) SPUtils.get(context, "substations", ""));
        list.setLayoutManager(RecyclerViewUtils.getLinearLayoutManagerForVertical(context));
        list.setAdapter(adapter = new SuperAdapter<Defect>(R.layout.content_defect) {
            @Override
            public void bindView(ViewHolder viewHolder, final Defect s, int i) {
                ((TextView) viewHolder.getView(R.id.substation)).setText((String) SPUtils.get(context, "substations", ""));
                ((TextView) viewHolder.getView(R.id.lv)).setText("(" + s.getDefectLevelvalue() + ")");
                ((TextView) viewHolder.getView(R.id.description)).setText(s.getDefectDesc());
                List<Eqpt> eqpts = GreenDaoManager.getInstance().getmDaoSession().getEqptDao().queryBuilder().where(EqptDao.Properties.EqptGuid.eq(s.getEqptGuid())).list();
                ((TextView) viewHolder.getView(R.id.eqpt)).setText(eqpts.size() > 0 ? eqpts.get(0).getEqptName() : "未知设备");
                ((TextView) viewHolder.getView(R.id.finds)).setText(s.getDiscoveryUserIdvalue());
                ((TextView) viewHolder.getView(R.id.time)).setText(s.getDefectTime());
                Button process = viewHolder.getView(R.id.process);
                if (s.getDefectStatus().equals("20")) {
                    process.setBackgroundResource(android.R.color.darker_gray);
                    process.setClickable(false);
                    process.setText("已消缺");
                } else if (s.getDefectFrom().equals("新增")) {
                    process.setClickable(true);
                    process.setText("删除");
                    process.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            datas.remove(s);
                            GreenDaoManager.getInstance().getmDaoSession().getDefectDao().delete(s);
                            List<Inspect> inspects = GreenDaoManager.getInstance().getmDaoSession().getInspectDao()
                                    .queryBuilder().where(InspectDao.Properties.ItemGuid.eq(s.getInspectGuid())).list();
                            if (inspects.size() > 0) {
                                Inspect inspect = inspects.get(0);
                                inspect.setDefectCount(inspect.getDefectCount() - 1);
                                if (inspect.getDefectCount() == 0) inspect.setConclusion("00");
                                GreenDaoManager.getInstance().getmDaoSession().getInspectDao().update(inspect);
                                unit = JSON.parseObject((String) SPUtils.get(context, "unit", ""), Unit.class);
                                unit.setExcount(unit.getExcount() - 1);
                                SPUtils.put(context, "unit", JSON.toJSONString(unit));
                                GreenDaoManager.getInstance().getmDaoSession().getUnitDao().update(unit);
                                ((PatroDetailActivity) getActivity()).refreshData();
                            } else {
                                adapter.notifyDataSetChanged();
                                setDefect();
                            }
                        }
                    });
                } else {
                    process.setClickable(true);
                    process.setText("消缺");
                    process.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            SPUtils.put(context, "work", true);//是否是从巡视任务过来的
                            SPUtils.put(context, "defect", JSON.toJSONString(s));
                            startActivity(new Intent(context, RemoveDefectActivity.class));
                        }
                    });
                }
                RecyclerView recyclerView = viewHolder.getView(R.id.list);
                recyclerView.setLayoutManager(RecyclerViewUtils.getLinearLayoutManagerForHorizontal(context));
                List<Photo> photos = GreenDaoManager.getInstance().getmDaoSession().getPhotoDao().queryBuilder().where(PhotoDao.Properties.DefectGuid.eq(s.getDefectGuid())).list();
                SuperAdapter<Photo> adapter = new DefectImageAdapter(getActivity(), photos);
                recyclerView.setAdapter(adapter);
                adapter.setData(photos);
                adapter.setEmptyDataView(R.layout.view_empty);
            }
        });
        adapter.setData(datas);
        getDatas();
    }

    public void getDatas() {
        datas.clear();
        datas.addAll(GreenDaoManager.getInstance().getNewSession().getDefectDao().queryBuilder().
                where(DefectDao.Properties.SubsGuid.eq(task.getSubsGuid()), DefectDao.Properties.WorkGuid.eq(task.getWorkGuid())).orderDesc(DefectDao.Properties.DefectTime).list());
        adapter.notifyDataSetChanged();
        setDefect();
    }

    @OnClick(R.id.add)
    public void onViewClicked() {
        SPUtils.put(context, "defect_content", unit.getTourUnit());
        startActivity(new Intent(context, DefectAddActivity.class));
    }

    void setDefect() {
        total = datas.size();
        ready = 0;
        for (Defect defect : datas) {
            if (defect.getDefectStatus().equals("20")) {
                ready++;
            }
        }
        totalCount.setText(total + "");
        readCount.setText(ready + "");
    }

}
